package Manager;
import Manager.Product;
import User.Order;

import java.io.*;
import java.util.List;
import java.util.Scanner;

public class Admin {
    private static final String SALES_TOTAL_FILE = "sales_total.txt";

    public static void addProduct(List<Product> catalog) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入商品名称: ");
        String name = scanner.nextLine();
        System.out.print("请输入商品价格: ");
        double price = scanner.nextDouble();
        System.out.print("请输入商品库存: ");
        int stock = scanner.nextInt();
        int num=0;
        catalog.add(new Product(name, price, stock,num));
        System.out.println("商品已添加: " + name);
    }

    public static void deleteProduct(List<Product> catalog) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的商品名称: ");
        String name = scanner.nextLine();
        Product toRemove = null;
        for (Product product : catalog) {
            if (product.name.equals(name)) {
                toRemove = product;
                break;
            }
        }
        if (toRemove != null) {
            catalog.remove(toRemove);
            System.out.println("商品已删除: " + name);
        } else {
            System.out.println("未找到商品: " + name);
        }
    }

    public static void updateProduct(List<Product> catalog) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要修改的商品名称: ");
        String name = scanner.nextLine();
        Product toUpdate = null;
        for (Product product : catalog) {
            if (product.name.equals(name)) {
                toUpdate = product;
                break;
            }
        }
        if (toUpdate != null) {
            System.out.print("请输入新的商品价格: ");
            double price = scanner.nextDouble();
            System.out.print("请输入新的商品库存: ");
            int stock = scanner.nextInt();
            toUpdate.price = price;
            toUpdate.stock = stock;
            System.out.println("商品已更新: " + name);
        } else {
            System.out.println("未找到商品: " + name);
        }
    }

    public static void queryProduct(List<Product> catalog) {
        System.out.println("商品目录:");
        for (Product product : catalog) {
            System.out.println(product);
        }
    }

    public static void salesStatistics(List<Order> orders) {
        double totalSales = loadTotalSales();  // 从文件加载销售总额
        for (Order order : orders) {
            if (order.isPaid) {
                totalSales += order.getTotal();
            }
        }
        saveTotalSales(totalSales);  // 保存更新后的销售总额
        System.out.println("总销售额: ¥" + totalSales);
    }

    public static void stockStatistics(List<Product> catalog) {
        System.out.println("商品库存统计:");
        for (Product product : catalog) {
            System.out.println(product.name + " - 库存: " + product.stock);
        }
    }

    private static double loadTotalSales() {
        double totalSales = 0.0;
        try (BufferedReader reader = new BufferedReader(new FileReader(SALES_TOTAL_FILE))) {
            String line = reader.readLine();
            if (line != null) {
                totalSales = Double.parseDouble(line);
            }
        } catch (IOException e) {
            System.out.println("读取销售总额文件失败: " + e.getMessage());
        }
        return totalSales;
    }

    private static void saveTotalSales(double totalSales) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(SALES_TOTAL_FILE))) {
            writer.write(String.valueOf(totalSales));
        } catch (IOException e) {
            System.out.println("保存销售总额文件失败: " + e.getMessage());
        }
    }
}
